package com.idea.plugin.action;

import com.idea.plugin.core.domain.dto.JavaClassDTO;
import com.idea.plugin.core.domain.info.JavaClassInfo;
import com.idea.plugin.core.helper.JavaClassHelper;
import com.idea.plugin.core.resolver.UnitTestingClassResolver;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.DataKey;
import com.intellij.openapi.module.ModuleUtil;
import com.intellij.openapi.module.impl.ModuleImpl;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiClass;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yanzhao
 * @date 2022/2/16 10:23
 * @since 1.0.0
 */
public class BatchTestingAction extends AnAction {

    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {
        final VirtualFile virtualFile = CommonDataKeys.VIRTUAL_FILE.getData(e.getDataContext());
        if (virtualFile != null) {
            List<PsiClass> psiClasses = JavaClassHelper.getPsiClassRecursively(virtualFile);
            if (psiClasses.size() > 0) {
                ModuleImpl module = e.getData(DataKey.create("module"));
                List<JavaClassDTO> templateDataList = new ArrayList<>();
                for (PsiClass psiClass : psiClasses) {
                    JavaClassInfo javaClassInfo = new JavaClassInfo();
                    javaClassInfo.setPsiClass(psiClass);
                    javaClassInfo.setQualifiedName(JavaClassHelper.getMemberQualifiedName(psiClass));
                    javaClassInfo.setBaseDir(ModuleUtil.getModuleDirPath(module));
                    javaClassInfo.setAbsolutePath(psiClass.getContainingFile().getVirtualFile().getCanonicalPath());
                    try {
                        JavaClassDTO javaClassDTO = UnitTestingClassResolver.build(javaClassInfo);
                        if (javaClassDTO != null) {
                            templateDataList.add(javaClassDTO);
                        }
                        System.out.println();
                    } catch (IOException ioException) {
                        ioException.printStackTrace();
                    }
                }
            }
        }

    }

    @Override
    public void update(@NotNull AnActionEvent e) {

    }
}
